package com.hnevc.moshuizhu1;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONException;
import org.json.JSONObject;

public class ZhuceActivity extends AppCompatActivity {
    private EditText zhanghao1;
    private EditText mima1;
    private Button tijiao;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhuce_main);
        initView();
        initEvent();
    }

    private void initView() {
        zhanghao1 = findViewById(R.id.zhanghao1);
        mima1 = findViewById(R.id.mima1);
        tijiao =findViewById(R.id.tijiao);
    }

    private void initData() {
        String url = "http://10.1.62.39/api/user/register";
        String telephone = zhanghao1.getText().toString().trim();
        String password = mima1.getText().toString().trim();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("telephone",telephone);
            jsonObject.put("name","莫水珠");
            jsonObject.put("password",password);

        }catch (JSONException e){
            e.printStackTrace();
        }
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Toast.makeText(ZhuceActivity.this,jsonObject.optString("msg")+"",Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(ZhuceActivity.this,"网络请求失败!",Toast.LENGTH_SHORT).show();
            }
        });
        RequestQueue queue= Volley.newRequestQueue(ZhuceActivity.this);
        queue.add(jsonObjectRequest);
    }

    private void initEvent() {
        tijiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
                Intent intent = new Intent(ZhuceActivity.this, DenluActivity.class);
                startActivity(intent);
            }
        });
    }

}
